﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class ContragentIPEditForm : Form
    {
        private string mode;

        private Form parentForm;

        public Contragent contragent;

        private bool act = false;

        public ContragentIPEditForm()
        {
            InitializeComponent();
        }

                 /// <summary>
        /// Конструктор на добавление
        /// </summary>
        /// <param name="parentForm"></param>
        /// <param name="mode"></param>
        public ContragentIPEditForm(Form parentForm, string mode)
        {
            InitializeComponent();

            this.mode = mode;

            this.Text = string.Format("Контрагент (Индивидуальный предприниматель) [{0}]", mode);

            this.parentForm = parentForm;

            if (this.mode == "Добавление")
            {
                contragent = new Contragent();
                act = true;
            }
        }

        /// <summary>
        /// Конструктор на изменение
        /// </summary>
        /// <param name="parentForm"></param>
        /// <param name="mode"></param>
        /// <param name="id"></param>
        public ContragentIPEditForm(Form parentForm, string mode, int id, string type)
        {
            InitializeComponent();

            this.mode = mode;

            this.Text = string.Format("Контрагент (Индивидуальный предприниматель) [{0}]", mode);

            this.parentForm = parentForm;

            if (mode == "Изменение")
            {
                contragent = new Contragent();

                Tools.RunWithProgressIndicator(this, delegate
                {
                    contragent.typeContragent = type;

                    contragent.SelectById(id, this.contragent.typeContragent);

                    mtbxINN.Text = contragent.IPINN;
                    tbxIPName.Text = contragent.IPName;
                    tbxRSchet.Text = contragent.IPRSchet;
                    tbxKSchet.Text = contragent.IPKSchet;
                    tbxEGRIP.Text = contragent.IPEGRIP;

                    tbxTelWork.Text = contragent.telWork;
                    tbxTelMobile.Text = contragent.telMobile;
                    tbxNote.Text = contragent.note;
                    tbxEmail.Text = contragent.email;

                    tbxAddressText.Text = contragent.address.RebuildAddressText();

                });
            }

            this.act = true;
        }

        private void rebuildContragentData(object sender, EventArgs e)
        {
            if (act)
            {
                this.contragent.IPName = tbxIPName.Text.Trim();
                this.contragent.IPINN = mtbxINN.Text;
                this.contragent.IPEGRIP = tbxEGRIP.Text.Trim(); 
                this.contragent.IPRSchet = tbxRSchet.Text.Trim();
                this.contragent.IPKSchet = tbxKSchet.Text.Trim();

                this.contragent.telHome = tbxTelHome.Text.Trim();
                this.contragent.telMobile = tbxTelMobile.Text.Trim();
                this.contragent.telWork = tbxTelWork.Text.Trim();
                this.contragent.note = tbxNote.Text.Trim();
                this.contragent.email = tbxEmail.Text.Trim();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            bool bSuccessfully = false;

            try
            {
                contragent.typeContragent = "ИП";

                if (this.mode == "Изменение")
                {
                    Tools.RunWithProgressIndicator(this, delegate { contragent.Update(); });

                    MessageBox.Show("Контрагент изменен успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }
                else
                {
                    if (mode == "Добавление")
                    {
                        Tools.RunWithProgressIndicator(this, delegate { contragent.Insert(); });

                        MessageBox.Show("Контрагент добавлен успешно", "Информация", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }

                bSuccessfully = true;
            }
            catch
            {
                ;
            }

            if (bSuccessfully)
            {
                this.Close();
            }
        }

        private void AddressChange(TextBox tbxAddress)
        {
            AddressEditForm addressEditForm;

            if (contragent.address.id_address != 0)
            {
                addressEditForm = new AddressEditForm(this, "Изменение", contragent.address.id_address);
            }
            else
            {
                addressEditForm = new AddressEditForm(this, "Добавление");
            }

            addressEditForm.ShowDialog();

            tbxAddress.Text = addressEditForm.address.RebuildAddressText();
            contragent.address = addressEditForm.address;
        }

        private void btnAddressChange_Click_1(object sender, EventArgs e)
        {
            AddressChange(tbxAddressText);
        }

        private void btnAddressChangeTwo_Click(object sender, EventArgs e)
        {
            AddressChange(tbxAddressTwo);
        }



    }
}
